#include "../includes/state_machine.h"
#include "../includes/processor.h"

#include "miwi/WirelessProtocols/Console.h"
#include "miwi/ConfigApp.h"
#include "miwi/HardwareProfile.h"
#include "miwi/WirelessProtocols/MCHP_API.h"
#include "miwi/WirelessProtocols/LCDBlocking.h"

#include "miwi/WirelessProtocols/SymbolTime.h"

BYTE myChannel = 25;

void miwi_init(){
    BYTE i;
    BYTE TxSynCount = 0;
    BYTE TxSynCount2 = 0;
    BYTE TxNum = 0;
    BYTE RxNum = 0;

    /*******************************************************************/
    // Initialize the system
    /*******************************************************************/
    BoardInit();      
    //ConsoleInit(); 

    /*******************************************************************/
    // Function MiApp_ProtocolInit initialize the protocol stack. The
    // only input parameter indicates if previous network configuration
    // should be restored. In this simple example, we assume that the 
    // network starts from scratch.
    /*******************************************************************/
    MiApp_ProtocolInit(FALSE);

    // Set default channel
    if( MiApp_SetChannel(myChannel) == FALSE )
    {
		//FAIL... hope that we never enter this block!!!
    }

    /*******************************************************************/
    // Function MiApp_ConnectionMode defines the connection mode. The
    // possible connection modes are:
    //  ENABLE_ALL_CONN:    Enable all kinds of connection
    //  ENABLE_PREV_CONN:   Only allow connection already exists in 
    //                      connection table
    //  ENABL_ACTIVE_SCAN_RSP:  Allow response to Active scan
    //  DISABLE_ALL_CONN:   Disable all connections. 
    /*******************************************************************/
    MiApp_ConnectionMode(ENABLE_ALL_CONN);

    /*******************************************************************/
    // Function MiApp_EstablishConnection try to establish a new 
    // connection with peer device. 
    // The first parameter is the index to the active scan result, 
    //      which is acquired by discovery process (active scan). If 
    //      the value of the index is 0xFF, try to establish a 
    //      connection with any peer.
    // The second parameter is the mode to establish connection, 
    //      either direct or indirect. Direct mode means connection 
    //      within the radio range; indirect mode means connection 
    //      may or may not in the radio range. 
    /*******************************************************************/
    #ifdef ENABLE_HAND_SHAKE
        i = MiApp_EstablishConnection(0xFF, CONN_MODE_DIRECT);
    #endif
}